package monster.com.cvh.adapter;

import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.bean.ResumeTemplateBean;
import monster.com.cvh.event.RefreshResumeTempleIdEvent;
import monster.com.cvh.listener.ItemClickListener;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.view.ResumeImageView;

/**
 * @author: 13580
 * @data: 2018/4/10
 * @function:简历模板小图预览Adapter
 */
public class ResumeTemplatePreviewAdapter extends BaseQuickAdapter<ResumeTemplateBean, BaseViewHolder> {
    private ItemClickListener mItemClickListener;
    private int mCurrentIndex = 0;

    public ResumeTemplatePreviewAdapter(List<ResumeTemplateBean> data, ItemClickListener listener) {
        super(R.layout.item_resume, data);
        this.mItemClickListener = listener;
        if (data!=null&&data.size()>0){
            initTemplateId(data.get(0).getId());
            initTemplateLock(data.get(0).isLock());
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, final ResumeTemplateBean item) {
        ResumeImageView resumeImageView = (ResumeImageView) helper.getView(R.id.iv_item_resume);
        final int position = helper.getLayoutPosition();
        Glide.with(mContext)
                .load(item.getPath())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(resumeImageView);
        if (mCurrentIndex == position) {
            resumeImageView.setHasBorder(true);
            helper.setGone(R.id.iv_item_resume_checked, true);
        } else {
            resumeImageView.setHasBorder(false);
            helper.setGone(R.id.iv_item_resume_checked, false);
        }

        resumeImageView.setmHasLock(item.isLock());

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initTemplateId(item.getId());
                initTemplateLock(item.isLock());
                String token = SPUtils.getString(mContext, MyConstant.TOKEN, "");
                if (token.isEmpty()) {
                    mItemClickListener.loadBigImage(MyConstant.RESUME_TEMPLATES_URL + item.getId());
                } else {
                    mItemClickListener.loadBigImage(MyConstant.RESUME_TEMPLATES_URL + item.getId() + "?token=" + token);
                }
                mCurrentIndex = position;
                notifyDataSetChanged();
            }
        });
    }


    /**
     * 更新简历缩略图
     *
     * @param date
     */
    public void addNewResume(List<ResumeTemplateBean> date) {
        setNewData(date);
        ResumeTemplateBean item = date.get(0);
        initTemplateLock(item.isLock());
        mItemClickListener.loadBigImage(MyConstant.RESUME_TEMPLATES_URL + item.getId() + "?token=" + SPUtils.getString(mContext, MyConstant.TOKEN, ""));
        mCurrentIndex = 0;
        initTemplateId(item.getId());
        notifyDataSetChanged();
    }

    /**
     * 刷新templateId
     *
     * @param id
     */
    private void initTemplateId(int id) {
        RefreshResumeTempleIdEvent.getInstance().onRefreshResumeTempleId(id);
    }

    /**
     * 刷新templateId
     *
     * @param hasLock
     */
    private void initTemplateLock(boolean hasLock) {
        RefreshResumeTempleIdEvent.getInstance().onRefreshResumeTempleLock(hasLock);
    }
}
